package org.groupg.poi.word;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class WordProcessor {
    public static void processWordFile(String inputFilePath, String outputFilePath, String placeholder, String replacement) {
        try (FileInputStream fis = new FileInputStream(inputFilePath);
             XWPFDocument document = new XWPFDocument(fis);
             FileOutputStream fos = new FileOutputStream(outputFilePath)) {
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                for (int i = 0; i < paragraph.getRuns().size(); i++) {
                    String text = paragraph.getRuns().get(i).getText(0);
                    text = text.replaceAll(placeholder, replacement);
                    paragraph.getRuns().get(i).setText(text, 0);
                }
            }
            document.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}